package com.qfhd.soft.cjzcore.Core;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CJJsonUtils {
    public static String getValueFromKey_string(JSONObject obj, String key){
        if(obj.has(key)){
            try {
                return obj.getString(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return "";
            }
        }
        else{
            return "";
        }
    }

    public static int getValueFromKey_int(JSONObject obj,String key){
        if(obj.has(key)){
            try {
                return obj.getInt(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return 0;
            }
        }
        else{
            return 0;
        }
    }

    public static long getValueFromKey_long(JSONObject obj,String key){
        if(obj.has(key)){
            try {
                return obj.getLong(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return 0;
            }
        }
        else{
            return 0;
        }
    }

    public static float getValueFromKey_float(JSONObject obj,String key){
        if(obj.has(key)){
            try {
                return (float) obj.getDouble(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return 0;
            }
        }
        else{
            return 0;
        }
    }

    public static JSONObject getJSONObjectFromKey_string(JSONObject obj, String key){
        if(obj.has(key)){
            try {
                return obj.getJSONObject(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
        else{
            return null;
        }
    }

    public static boolean getValueFromKey_boolean(JSONObject obj, String key){
        if(obj.has(key)){
            try {
                return obj.getBoolean(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return false;
            }
        }
        else{
            return false;
        }
    }

    public static List<Boolean> getListFromKey_boolean(JSONObject obj, String key){
        List<Boolean> tmp = new ArrayList<>();
        if(obj.has(key)){
            try {
                JSONArray array = obj.getJSONArray(key);
                for(int i=0; i<array.length(); i++){
                    tmp.add(array.getBoolean(i));
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return tmp;
            }
        }
        return tmp;
    }

    public static JSONArray getJSONArrayFromKey(JSONObject obj, String key){
        JSONArray tmp = new JSONArray();
        if(obj.has(key)){
            try {
                tmp = obj.getJSONArray(key);
            } catch (JSONException e) {
                e.printStackTrace();
                return tmp;
            }
        }
        return tmp;
    }

}
